<template>
  <div id="zong">
    <el-form
      :model="Form"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="Form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="Form.password"></el-input>
      </el-form-item>
      <el-button @click="postitem">登录</el-button>
    </el-form>
  </div>
</template>


<script>
// import axios from "../../node_modules/axios/dist/axios";
export default {
  data() {
    return {
      Form: {
        name: "admin",
        password: "123456",
      },

      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 7, message: "长度在 4 到 7 个字符", trigger: "blur" },
        ],
      },
    };
  },

  mounted() {},

  methods: {
    postitem() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          alert("格式有错误");
          return false;
        } else {
          this.$axios
            .post("/login", {
              username: this.Form.name,
              password: this.Form.password,
            })
            .then((res) => {
              console.log(res);
              let resData = res.data.meta;
              let resd = res.data.data;
   
              if (res.data.meta.status == 200) {
                this.$message({
                  message: "登陆成功",
                  type: "success",
                });
                this.$store.commit("userinfo", res.data.data);
                this.$router.push({
                  path: "/home",
                });
              } else {
                this.$message({
                  message: res.data.meta.msg,
                  type: "warning",
                });
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="css">
* {
  margin: 0;
  padding: 0;
}
body {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
#zong {
  width: 400px;
  margin: 0 auto;
}
</style>